// cd /projects/hsieh_project/proj_201809/code_2_202011/
// qstata mkt_d_ind_aux_imp.do &

set linesize 255
capture log close
log using /projects/hsieh_project/proj_201809/code_2_202011/mkt_d_ind_aux_imp_log, replace text

clear all
cd /projects/

di "Started at $S_TIME $S_DATE"

global rev_date: display %tdYYNNDD date("$S_DATE", "DMY")
di "${rev_date}"

global dir_proj "/projects/hsieh_project/proj_201809/"

global dir_do "${dir_proj}/code_2_202011/"
global dir_data "${dir_proj}/data/"
global dir_out "${dir_proj}/output/202011_main/"
noi capture mkdir ${dir_out}
global dir_outf "${dir_out}/mkt_d_ind_aux_imp/"
noi capture mkdir ${dir_outf}

global year1 = 1977
global year2 = 2013

global gl_perc = "10"
local l_perc = "10"

global gl_def = "1 2 54 55"

do "/projects/hsieh_project/code_0_general/f_rounding.do"
do "/projects/hsieh_project/code_0_general/f_reg.do"

global ds_ind "${dir_data}/ind_sum_all"

global lvmkt = "est msa1983 zipcode fips "
global vmkt_sel = "msa1983"

tempfile ds_temp
global ds_temp = "`ds_temp'"

//==============================================================================

do ${dir_do}/mkt_d_ind_0.do "geo"
save ${dir_outf}/mkt_d_ind_aux_imp, replace

//==============================================================================
// Calculate auxiliary employment in each industry

capture program drop f_aux
program f_aux
keep if inlist(year,${year1},${year2})

rename emp_ind_aux emp_aux
rename est_ind_aux est_aux
rename pay_ind_aux pay_aux
keep aux_def year ch_ind i_aux_ind emp_aux est_aux pay_aux

merge m:1 year ch_ind using ${ds_ind}, keepus(emp_ind est_ind pay_ind) // Get industry totals
keep if _merge == 3

// Assign negative value to year1 to calculate change
foreach vvar in emp est pay {
gen ln_`vvar'_aux = ln(`vvar'_aux)
replace `vvar'_aux = - `vvar'_aux if year == ${year1}
replace ln_`vvar'_aux = - ln_`vvar'_aux if year == ${year1}

gen ln_`vvar's_aux = ln(`vvar'_aux/`vvar'_ind)
replace ln_`vvar's_aux = - ln_`vvar's_aux if year == ${year1}
}

gen n_year = 1

// Calculate change
collapse (sum) emp_aux ln_emp_aux ln_emps_aux est_aux ln_est_aux ln_ests_aux ln_pay_aux ln_pays_aux n_year, by(aux_def ch_ind i_aux_ind)
tab ch_ind if n_year == 1
drop if n_year == 1
end

use ${dir_data}/aux_ind_sum, clear
f_aux
save ${ds_temp}_a, replace

merge m:1 ch_ind using ${dir_outf}/mkt_d_ind_aux_imp
tab ch_ind if _merge != 3
keep if sector != .

keep if i_aux_ind == .

//==============================================================================
// Function that produces figures

capture program drop fig_aux
program fig_aux

	args y y_lab fig_rt

foreach idef in $gl_def {
    noi capture mkdir ${dir_outf}/def`idef'/
    noi capture mkdir ${dir_outf}/def`idef'/data/
    preserve
    keep if aux_def == `idef'
    
foreach vmkt in $lvmkt {
	
	//local vmkt = "msa"
	//local y = "ln_emp_aux" 
	local x = "ln_mu_mkt_`vmkt'_d"
	//local y_lab = `"{&Delta} ln(Auxiliary Employment)"'
	local x_lab = `"{&Delta} ln(Average # of `=upper("`vmkt'")' of All Firms)"'
	
	// Run regression
    reg `y' `x' [aw=w_sv_numerator]
    f_reg_append "fr_reg" "`x'"
	frame fr_reg {
		replace aux_def = `idef' if _n == _N
	}
    
    local b_x: di %4.2f _b[`x'] // round(_b[`x'],0.001)
    local se_x: di %4.2f _se[`x']
    local reg_N = `e(N)'
    
    *Rounding to 4sf for plots (to be disclosure compliant)
    capture gen `y'_4sf = `y'
    capture gen `x'_4sf = `x'
    rounding_4dig "`y'_4sf `x'_4sf"
    sum `y' `x' `y'_4sf `x'_4sf
    

    /*
	twoway (scatter `y' `x', msize(vsmall)) ///
		(lpolyci `y' `x' [aw=w_sv_numerator], level(99) lcolor(maroon) lw(med) fi(inten0)), ///
		ytitle("`y_lab'") xtitle("`x_lab'") ///
		legend(off) note("99% CI, Def `idef'; _b[x] = `b_x', _se[x] = `se_x'")
		*/
		
	twoway ///
		(lpolyci `y'_4sf `x'_4sf [aw=w_sv_numerator], level(99) lcolor(maroon) lw(med) fi(inten0)), ///
		ytitle("`y_lab'") xtitle("`x_lab'") ///
		legend(off) note("99% CI, Def `idef'; _b[x] = `b_x', _se[x] = `se_x'")
		
	//graph export ${dir_outf}/def`idef'/Y_l_emp_aux_d_X_l_mkt_d_`vmkt'.png, replace width(3000) height(2000)
	graph export ${dir_outf}/def`idef'/`fig_rt'_`vmkt'.png, replace width(3000) height(2000)
    
    local b_x_`vmkt': di %4.2f `b_x'
    local se_x_`vmkt': di %4.2f `se_x'
    


}
	keep *_4sf w_sv_numerator
    save "${dir_outf}/def`idef'/data/`y'_`x'.dta", replace
/*
    //local y = "ln_emp_aux" 
	local x1 = "ln_mu_mkt_est_d"
    local x2 = "ln_mu_mkt_fips_d"
    local x3 = "ln_mu_mkt_msa1983_d"
    local x4 = "ln_mu_mkt_zip_d"
	//local y_lab = `"{&Delta} ln(Auxiliary Employment)"'
	local x_lab = `"{&Delta} ln(Average # of Markets of All Firms)"'
	
	/*
	twoway (lpoly `y' `x1' [aw=w_emps], lw(med) fi(inten0)) ///
        (lpoly `y' `x2' [aw=w_emps], lw(med) fi(inten0)) ///
        (lpoly `y' `x3' [aw=w_emps], lw(med) fi(inten0)) ///
        (lpoly `y' `x4' [aw=w_emps], lw(med) fi(inten0)) ///
        (lpoly `y' `x5' [aw=w_emps], lw(med) fi(inten0)), ///
		ytitle("`y_lab'") xtitle("`x_lab'") ///
		legend(order(1 "Est" 2 "FIPS" 3 "MSA" 4 "ZIP" 5 "CZONE") rows(1)) ///
        note("Def `idef'")
		//note("Def `idef'; Est: _b (_se)= `b_x_est' (`se_x_est'); FIPS: `b_x_fips' (`se_x_fips'); MSA: `b_x_msa' (`se_x_msa'); ZIP: `b_x_zip' (`se_x_zip'); CZONE: `b_x_czone' (`se_x_czone')")
     
    graph export ${dir_outf}/def`idef'/z_`fig_rt'_all.png, replace width(3000) height(2000)
	*/
    

    twoway (lpoly `y' `x1' [aw=w_sv_numerator], lw(med) fi(inten0)) ///
        (lpoly `y' ln_mu_mkt_${vmkt_sel}_d [aw=w_sv_numerator], lw(med) fi(inten0)), ///
		ytitle("`y_lab'") xtitle("`x_lab'") ///
		legend(order(1 "Est" 2 "${vmkt_sel}") rows(1)) ///
        note("Def `idef'")
		//note("Def `idef'; Est: _b (_se)= `b_x_est' (`se_x_est'); CZONE: `b_x_czone' (`se_x_czone')")
     
    graph export ${dir_outf}/def`idef'/z_`fig_rt'_sel.png, replace width(3000) height(2000)
    */ 
    restore
}
	
end

//==============================================================================
// Main results

f_reg_create "fr_reg" "1"
frame fr_reg {
	gen aux_def = .
	order aux_def
}

fig_aux "ln_emp_aux" `"{&Delta} ln(Auxiliary Employment)"' "Y_ln_emp_aux_d_X_ln_mkt_d"
fig_aux "ln_est_aux" `"{&Delta} ln(Auxiliary Establishments)"' "Y_ln_est_aux_d_X_ln_mkt_d"
fig_aux "ln_pay_aux" `"{&Delta} ln(Auxiliary Pay)"' "Y_ln_pay_aux_d_X_ln_mkt_d"


f_reg_save "fr_reg" "${dir_outf}/reg_Y_ln_ind_aux_d_X_ln_mkt_d"

//==============================================================================
// Experimental: Y = employment share

foreach idef in $gl_def {
    noi capture mkdir ${dir_outf}/def`idef'/
    preserve
    keep if aux_def == `idef'
    
foreach vmkt in $lvmkt {
	
	//local vmkt = "msa"
	local y = "ln_emps_aux" 
	local x = "ln_mu_mkt_`vmkt'_d"
	local y_lab = `"{&Delta} ln(Auxiliary / Industry Employment)"'
	local x_lab = `"{&Delta} ln(Average # of `=upper("`vmkt'")' of All Firms)"'
    
    reg `y' `x' [aw=w_sv_numerator]
    local b_x: di %4.2f _b[`x'] // round(_b[`x'],0.001)
    local se_x: di %4.2f _se[`x']
    local reg_N = `e(N)'
	
	twoway (scatter `y' `x', msize(vsmall)) ///
		(lpolyci `y' `x' [aw=w_sv_numerator], level(99) lcolor(maroon) lw(med) fi(inten0)), ///
		ytitle("`y_lab'") xtitle("`x_lab'") ///
		legend(off) note("99% CI, Def `idef'; _b[x] = `b_x', _se[x] = `se_x'")
		
	graph export ${dir_outf}/def`idef'/Y_l_emps_aux_d_X_l_mkt_d_`vmkt'.png, replace width(3000) height(2000)

}
    
    restore
}




di "Ended at $S_DATE $S_TIME"
capture log close
// End of do file
